package com.github_dididipapa.mall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.github_dididipapa.common.exception.BizCodeEnume;
import com.github_dididipapa.mall.product.entity.AttrAttrgroupRelationEntity;
import com.github_dididipapa.mall.product.entity.AttrEntity;
import com.github_dididipapa.mall.product.service.AttrService;
import com.github_dididipapa.mall.product.service.CategoryService;
import com.github_dididipapa.mall.product.service.impl.AttrAttrgroupRelationServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import com.github_dididipapa.mall.product.entity.AttrGroupEntity;
import com.github_dididipapa.mall.product.service.AttrGroupService;
import com.github_dididipapa.common.utils.PageUtils;
import com.github_dididipapa.common.utils.R;



/**
 * 属性分组
 *
 * @author dididipapa
 * @email not_found@404.com
 * @date 2025-04-26 18:51:12
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    private final AttrGroupService attrGroupService;
    private final CategoryService categoryService;
    private final AttrAttrgroupRelationServiceImpl attrAttrgroupRelationService;
    private final AttrService attrService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("categoryId") Long categoryId){
        PageUtils page = attrGroupService.queryPage(params, categoryId);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
        AttrGroupEntity attrGroup = null;
        try {
            attrGroup = attrGroupService.getById(attrGroupId);
            Long catelogId = attrGroup.getCatelogId();
            Long[] path = categoryService.findCatelogPath(catelogId);
            attrGroup.setCatelogPath(path);
        } catch (NullPointerException e) {
            log.info("NullPointExcetion:没有对应的{}属性组", attrGroupId);
             return R.error(BizCodeEnume.ATTRIBUTE_NOT_EXIST_EXCEPTION.getCode(),
                            BizCodeEnume.ATTRIBUTE_NOT_EXIST_EXCEPTION.getMsg());
        }

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    //获取分类下所有分组 & 关联属性
     @GetMapping("/{catelogId}/withattr")
     public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        //1.查询当前分类下的所有属性分组
        //2.查询每个属性分组下的所有属性
        List<AttrGroupEntity> data = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data", data);
    }

    @GetMapping("/{attrGroupId}/attr/relation")
    public R getAttrByAttrGroupId(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> data = attrGroupService.getAttrByAttrGroupId(attrGroupId);
        return R.ok().put("data",data);
    }

    @GetMapping("/{attrGroupId}/noattr/relation")
    public R getNoAttrByAttrGroupId(@RequestParam Map<String, Object> params,
                                    @PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page =  attrService.getNoAttrByAttrGroupId(attrGroupId, params);
        return R.ok().put("page", page);
    }

    @PostMapping("/attr/relation")
    public R addAttrGroupRelation(@RequestBody AttrAttrgroupRelationEntity[] relationEntity){
        for(AttrAttrgroupRelationEntity entity : relationEntity){
            attrAttrgroupRelationService.saveDetail(entity);
        }
        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    public R deleteAttrGroupRelation(@RequestBody AttrAttrgroupRelationEntity[] relationEntity){
        for(AttrAttrgroupRelationEntity entity : relationEntity){
            attrAttrgroupRelationService.deleteDetail(entity);
        }
        return R.ok();
    }
}
